<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/3/28
 * Time: 上午9:03
 */

namespace app\common\controller;


use app\common\model\Agent;
use app\common\model\CardGeneral;
use app\common\model\CardMember;
use app\common\model\Merchant;
use app\common\model\WxOpen;
use think\Controller;


class Wxoption extends Controller
{
    public function wxoption($type,$param){
        switch($type){
            case "mid":
               $params=$param;
                break;
            case "top_mid":
                $params=$param;
                break;
            case "gh_id":
                $params=$param;
                break;
            case "card_id":
                $params=CardMember::where('card_id',$param)->value('mid');
                break;
        }
        $map['id']=$params;
        $WxOpen=new WxOpen();
        $Agent=new Agent();
        $Merchant=new Merchant();
        $merchantinfo=$Merchant->where($map)->field('wx_open_id,agent')->find();
        $mab['id'] =$merchantinfo['wx_open_id'];
        $config =$Agent->where(array('id' => $merchantinfo['agent']))->find();
        $users = $WxOpen->where($mab)->find();
        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
            'authorizer_appid' => $users['authorizer_appid'],
            'authorizer_refresh_token' => $users['authorizer_refresh_token'],
        );
        return $options;


    }
    //微信API参数  根据card_id获取
    public function options($param,$type){
        switch($type){
            case "card_id":
                $CardMember = new CardMember();
                $Merchant = new Merchant();
                $Agent=new Agent();
                $WxOpen=new WxOpen();
                $map['id'] = $CardMember->where('card_id',$param)->value('mid');
                $merchantinfo=$Merchant->where($map)->field('wx_open_id,agent')->find();
                $mab['id'] = $merchantinfo['wx_open_id'];
                $config =$Agent->where(array('id' => $merchantinfo['agent']))->find();
                $users = $WxOpen->where($mab)->field('authorizer_appid,authorizer_refresh_token')->find();
                break;
            case "mid":
                $WxOpen=new WxOpen();
                $Agent=new Agent();
                $Merchant=new Merchant();
                $merchantinfo=$Merchant->where('id',$param)->field('wx_open_id,agent')->find();
                $mab['id'] =$merchantinfo['wx_open_id'];
                $config =$Agent->where(array('id' => $merchantinfo['agent']))->find();
                $users = $WxOpen->where($mab)->find();
                break;

        }


        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
            'authorizer_appid' => $users['authorizer_appid'],
            'authorizer_refresh_token' => $users['authorizer_refresh_token'],
        );
        return $options;
    }

    //获取所属商户
    public function get_topmid_mid($mid){
        $Merchant = new Merchant();
        $top_mid = $Merchant->where(['id'=>$mid])->value('top_mid');
        return $top_mid;
    }
    //获取商户名称
    public function getmerchantname($mid){
        $map['id']=$mid;
        $Merchant=new Merchant();
        $merchantname=$Merchant->where($map)->value('merchant');
        return $merchantname;

    }
    public function getdeduzkinfo($arr,$card_zk){
        $arr = json_decode($arr, JSON_UNESCAPED_UNICODE);
        $CardGeneral = new CardGeneral();
        if (is_array($arr)) {
            foreach ($arr as $k => $v) {
                if (isset($v['card_id'])) {
                    $datas[$k] = $CardGeneral->where(['card_id' => $v['card_id']])->value('title');
                } else {
                    $datas = [];
                }
            }
        }else{
            $datas = [];
        }

        if ($datas) {
            $data = implode(',', $datas).'('.$card_zk.'元)';
        } else {
            $data = NULL;
        }
        return $data;
    }
    //获取代理商域名()
    public function getagenturl_mid($mid){
        $Merchant = new Merchant();
        $Agent=new Agent();
        $agent_id = $Merchant->where(['id'=>$mid])->value('agent');
        $agent_url = $Agent->where(['id'=>$agent_id])->value('open_url');
        return 'https://'.$agent_url;
    }

}